#include "StdAfx.h"
#include ".\dsaudiodevice.h"

DSAudioDevice::DSAudioDevice(void)
{
}

DSAudioDevice::~DSAudioDevice(void)
{
}

void DSAudioDevice::Update( void )
{
	HRESULT hr = NOERROR;

	CComPtr<ICreateDevEnum> enumHardware;
	hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_ALL,
		IID_ICreateDevEnum, (void**)&enumHardware );
	if ( FAILED( hr ) )
		return;

	CComPtr<IEnumMoniker> enumMoniker;
	hr = enumHardware->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
		&enumMoniker, 0 );
	if ( FAILED( hr ) || !enumMoniker )
		return;

	enumMoniker->Reset();

	ULONG fetched = 0;
	CComPtr<IMoniker> moniker;

	while ( SUCCEEDED( enumMoniker->Next( 1, &moniker, &fetched ) ) && fetched )
	{
		if ( moniker )
		{
			WCHAR * dispName = 0;

			DSDevice::DSDevInfo devInfo;

			hr = moniker->GetDisplayName( NULL, NULL, &dispName );
			if ( SUCCEEDED( hr ) )
			{
				devInfo.DisplayName = dispName;
				CoTaskMemFree( dispName );
			}

			CComPtr<IPropertyBag> propertyBag;
			hr = moniker->BindToStorage( 0, 0, IID_IPropertyBag, (void **)&propertyBag );
			if ( SUCCEEDED( hr ) )
			{
				CComVariant friendlyName;
				friendlyName.vt = VT_BSTR;

				hr = propertyBag->Read( L"FriendlyName", &friendlyName, NULL );
				if ( SUCCEEDED( hr ) )
				{
					devInfo.FriendlyName = friendlyName.bstrVal;
				}
			}

			moniker.Release();
			m_devInfos.Add( devInfo );
		}
	}
}
